package com.lucweb.action;


import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.servlet.ServletContext;
import java.io.*;
import java.net.URLEncoder;

@Controller("downloadAction")
@Scope("prototype")
public class DownloadAction extends ActionSupport{

    private static final long serialVersionUID = 5879762231742395104L;
    private String fileName;//用户请求的文件名
    private String inputPath;//下载资源的路径(在struts配置文件中设置)
    public void setInputPath(String inputPath) {
        this.inputPath = inputPath;
    }
    public String getInputPath() {
        return inputPath;
    }
    public void setFileName(String fileName) {
        this.fileName = fileName;
    }
    public String getFileName() {
        return fileName;
    }
    public String downloadFile() throws Exception {
        ServletContext context = ServletActionContext.getServletContext();
        String downloadDir = context.getRealPath("/uploadFile");
        String downloadFile = context.getRealPath(inputPath);
//防止用户请求不安全的资源
        if(!downloadFile.startsWith(downloadDir)) {
            return null;
        }
        return "download_success";
    }
    /*
    * 获取输入流资源
    */
    public InputStream getInputStream() throws Exception {
        String path = inputPath + File.separatorChar + new String(fileName.getBytes("ISO-8859-1"), "UTF-8");
        return ServletActionContext.getServletContext().getResourceAsStream(path);
    }
    /*
    * 获取下载时文件默认的文件名
    */
    public String getDownloadFileName() {
        String downloadFileName = fileName;
        try {
            downloadFileName = URLEncoder.encode(downloadFileName, "ISO-8859-1");
        } catch (UnsupportedEncodingException e) {
            e.getMessage();
            e.printStackTrace();
        }
        return downloadFileName;
    }

}
